package jfreeChart.examples;

import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import mlFractal.functions.LogisticFunction;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;


/**
 * A simple demonstration application showing how to create a line chart using
 * data from an {@link XYDataset}.
 * <p>
 * IMPORTANT NOTE: THIS DEMO IS DOCUMENTED IN THE JFREECHART DEVELOPER GUIDE. DO
 * NOT MAKE CHANGES WITHOUT UPDATING THE GUIDE ALSO!!
 */
public class LineChartDemo2 extends ApplicationFrame {
	/**
	 * Creates a new demo.
	 * 
	 * @param title
	 *            the frame title.
	 */
	public LineChartDemo2(String title) {
		super(title);
		//XYDataset dataset = createDataset();
		Map<String, double[]> dataMap = new HashMap<String, double[]>();
		int iter = 200;
		LogisticFunction lf = new LogisticFunction(4, 0.74999);
		double[] lfData1 = lf.generateNumbers(iter);
		dataMap.put("(4;0.74999)", lfData1);
		lf = new LogisticFunction(4, 0.74990);
		double[] lfData2 = lf.generateNumbers(iter);
		dataMap.put("(4;0.74990)", lfData2);
		lf = new LogisticFunction(4, 0.75000);
		double[] lfData3 = lf.generateNumbers(iter);
		dataMap.put("(4;0.75000)", lfData3);
		
		lf = new LogisticFunction(1, 0.74999);
		lfData1 = lf.generateNumbers(iter);
		dataMap.put("(1;0.74999)", lfData1);
		lf = new LogisticFunction(2, 0.74999);
		lfData2 = lf.generateNumbers(iter);
		dataMap.put("(2;0.74999)", lfData2);
		lf = new LogisticFunction(3, 0.74999);
		lfData3 = lf.generateNumbers(iter);
		dataMap.put("(3;0.74999)", lfData3);
		
		XYDataset dataset = createDataset(dataMap);
		JFreeChart chart = createChart(dataset);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);
	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return a sample dataset.
	 */
	private static XYDataset createDataset(Map<String, double[]> dataMap) {
		Iterator<Map.Entry<String, double[]>> it = dataMap.entrySet().iterator();
		XYSeriesCollection dataset = new XYSeriesCollection();
		while(it.hasNext()){
			Map.Entry<String, double[]> entry = it.next();
			String key = entry.getKey();
			double[] values = entry.getValue();
		
			XYSeries series = new XYSeries(key);
			for(int i = 0; i < values.length; i++){
				series.add(i, values[i]);
			}
			dataset.addSeries(series);
		}
		return dataset;
	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            the data for the chart.
	 * 
	 * @return a chart.
	 */
	private static JFreeChart createChart(XYDataset dataset) {
		// create the chart...
		JFreeChart chart = ChartFactory.createXYLineChart("Line Chart Demo 2", // chart
																				// title
				"x", // x axis label
				"Y", // y axis label
				dataset, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips
				false // urls
				);
		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
		chart.setBackgroundPaint(Color.white);
		// get a reference to the plot for further customisation...
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
				.getRenderer();
		renderer.setShapesVisible(false);
		renderer.setShapesFilled(true);
		// change the auto tick unit selection to integer units only...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// OPTIONAL CUSTOMISATION COMPLETED.
		return chart;
	}

	/**
	 * Creates a panel for the demo (used by SuperDemo.java).
	 * 
	 * @return A panel.
	 *
	public static JPanel createDemoPanel() {
		JFreeChart chart = createChart(createDataset());
		return new ChartPanel(chart);
	}
	*/
	/**
	 * Starting point for the demonstration application.
	 * 
	 * @param args
	 *            ignored.
	 */
	public static void main(String[] args) {
		LineChartDemo2 demo = new LineChartDemo2("Line Chart Demo 2");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);
	}
}